﻿using System;
using System.Data;
using System.Data.SqlClient;
namespace PPI.Data
{
    public class DVendor_Visit_XMissionStructure
    {
        #region Query
        public static DataSet GetServerVisits(string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Vendor_Visit_XMissionStructures_SelectAllServer", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Vendor_Visit_XMissionStructures_SelectAllServer", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static DataSet GetServerVisitsByID(int structureID, string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Vendor_Visit_XMissionStructures_SelectAllByID", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_XMissionStructureId", ParameterDirection.Input, false, structureID));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Vendor_Visit_XMissionStructures_SelectAllByID", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static DataSet GetServerVisitsByQueryAsset(int queryAsset, string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Vendor_Visit_XMissionStructures_SelectAllServerByQueryAsset", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@QueryAssetFK", ParameterDirection.Input, false, queryAsset));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Vendor_Visit_XMissionStructures_SelectAllServerByQueryAsset", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }

        }

        public static DataSet GetServerVisitsByUtilityID(int utilityID, string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Vendor_Visit_XMissionStructures_SelectAllByUtility", DBHelper.GetConnection(connectionKey));
                if(utilityID == Int32.MinValue)
                    da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@UtilityID", ParameterDirection.Input, true, DBNull.Value));
                else
                    da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@UtilityID", ParameterDirection.Input, false, utilityID));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Vendor_Visit_XMissionStructures_SelectAllByUtility", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        #endregion Query

        #region Crud

        public static Int32 Insert(Int32 Vendor_ProjectID, Int32 Utility_Asset_XMissionStructureID, Int32 Vendor_Visit_TypeID, Int32 XMissionStructures_TopTypeID, double Latitude, double Logitude, double Alititude, Int32 SatelliteCount, DateTime SatelliteTime, bool SolutionAvaible, Int32 Handheld, Int32 Vendor_ContactID, DateTime date, int countImages, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Vendor_Visit_XMissionStructures_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_ProjectFK", ParameterDirection.Input, false, Vendor_ProjectID));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_TypeFK", ParameterDirection.Input, false, Vendor_Visit_TypeID));
                cmd.Parameters.Add(DBHelper.CreateBitParameter("@SolutionAvailable", ParameterDirection.Input, false, SolutionAvaible));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@CountImages", ParameterDirection.Input, false, countImages));

                #region Utility_Asset_XMissionStructureID
                if (Utility_Asset_XMissionStructureID == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Utility_Asset_XMissionStructureFK", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Utility_Asset_XMissionStructureFK", ParameterDirection.Input, false, Utility_Asset_XMissionStructureID));
                }
                #endregion

                #region XMissionStructure_TopTypeID
                if (XMissionStructures_TopTypeID == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@XMissionStructures_TopTypeFk", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@XMissionStructures_TopTypeFk", ParameterDirection.Input, false, XMissionStructures_TopTypeID));

                }
                #endregion

                #region Latitude
                if (Latitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, Latitude));
                }
                #endregion

                #region Longitude
                if (Logitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, Logitude));

                }
                #endregion

                #region Altitude
                if (Alititude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, false, Alititude));

                }
                #endregion

                #region SatelliteCount

                if (SatelliteCount == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, false, SatelliteCount));
                }



                #endregion

                #region SatelliteTime
                if (SatelliteTime == DateTime.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, SatelliteTime));
                }

                #endregion

                #region Handheld
                if (Handheld == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Handheld", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Handheld", ParameterDirection.Input, false, Handheld));

                }
                #endregion

                #region Vendor_ContactID
                if (Vendor_ContactID == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_ContactFK", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_ContactFK", ParameterDirection.Input, false, Vendor_ContactID));
                }
                #endregion

                #region Date
                if (date == DateTime.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@Date", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@Date", ParameterDirection.Input, false, date));

                }
                #endregion

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_XMissionStructureId", ParameterDirection.ReturnValue, false, 0));
                cmd.ExecuteNonQuery();
                tr.Commit();
                return (Int32)cmd.Parameters["@Vendor_Visit_XMissionStructureId"].Value;
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                return 0;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void Update(Int32 structureID, Int32 Vendor_ProjectID, Int32 Utility_Asset_XMissionStructureID, Int32 Vendor_Visit_TypeID, Int32 XMissionStructures_TopTypeID, double Latitude, double Logitude, double Alititude, Int32 SatelliteCount, DateTime SatelliteTime, bool SolutionAvaible, Int32 Handheld, Int32 Vendor_ContactID, DateTime date, int countImages, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Vendor_Visit_XMissionStructures_Update", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_XMissionStructureId", ParameterDirection.Input, false, structureID));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_ProjectFK", ParameterDirection.Input, false, Vendor_ProjectID));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_TypeFK", ParameterDirection.Input, false, Vendor_Visit_TypeID));
                cmd.Parameters.Add(DBHelper.CreateBitParameter("@SolutionAvailable", ParameterDirection.Input, false, SolutionAvaible));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@CountImages", ParameterDirection.Input, false, countImages));

                #region Utility_Asset_XMissionStructureID
                if (Utility_Asset_XMissionStructureID == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Utility_Asset_XMissionStructureFK", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Utility_Asset_XMissionStructureFK", ParameterDirection.Input, false, Utility_Asset_XMissionStructureID));
                }
                #endregion

                #region XMissionStructure_TopTypeID
                if (XMissionStructures_TopTypeID == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@XMissionStructures_TopTypeFk", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@XMissionStructures_TopTypeFk", ParameterDirection.Input, false, XMissionStructures_TopTypeID));

                }
                #endregion

                #region Latitude
                if (Latitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, Latitude));
                }
                #endregion

                #region Longitude
                if (Logitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, Logitude));

                }
                #endregion

                #region Altitude
                if (Alititude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, false, Alititude));

                }
                #endregion

                #region SatelliteCount

                if (SatelliteCount == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, false, SatelliteCount));
                }



                #endregion

                #region SatelliteTime
                if (SatelliteTime == DateTime.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, SatelliteTime));
                }

                #endregion

                #region Handheld
                if (Handheld == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Handheld", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Handheld", ParameterDirection.Input, false, Handheld));

                }
                #endregion

                #region Vendor_ContactID
                if (Vendor_ContactID == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_ContactFK", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_ContactFK", ParameterDirection.Input, false, Vendor_ContactID));
                }
                #endregion

                #region Date
                if (date == DateTime.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@Date", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@Date", ParameterDirection.Input, false, date));

                }
                #endregion

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void Delete(int structureID, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Vendor_Visit_XMissionStructures_Delete", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_XMissionStructureId", ParameterDirection.Input, false, structureID));
                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void UpdateUtility(Int32 structureID, Int32 utilityId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Vendor_Visit_XMissionStructures_UpdateUtility", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_XMissionStructureId", ParameterDirection.Input, false, structureID));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Utility_Asset_XMissionStructureFK", ParameterDirection.Input, false, utilityId));

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        #endregion

    }
}
